﻿using System.Reflection;

public sealed class MsgFactory
{
    private static readonly Dictionary<int, Type> m_RouteDict = new Dictionary<int, Type>();

    public static void Init(Assembly assembly)
    {
        m_RouteDict.Clear();
            
        foreach (var type in assembly.GetTypes())
        {
            if (!type.IsClass)
                continue;
                
            if (type.IsAbstract)
                continue;
                
            if (!type.IsSubclassOf(typeof(Message)))
                continue;
                
            var instance = Activator.CreateInstance(type) as Message;
            if (instance == null)
                continue;

            m_RouteDict.Add(instance.Route, type);
        }
    }

    public static Type FindType(int route)
    {
        return m_RouteDict.TryGetValue(route, out var type) ? type : null;
    }
}